﻿using Furion.DependencyInjection;
using Magic.Core;
using SqlSugar;
using System;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace Magic.FlowCenter
{
	public class FlcCustomFormService : IFlcCustomFormService, ITransient
    {
        private readonly ISqlSugarRepository<FlcCustomForm> _rep;
        private readonly IUserManager _userManager;

        public FlcCustomFormService(ISqlSugarRepository<FlcCustomForm> rep, IUserManager userManager)
        {
            _rep = rep;
            _userManager= userManager;
        }
        public async Task Add(long flowInstanceId, string frmData)
        {
            var req = frmData.ToObject<FlcCustomForm>();
            req.FlowInstanceId = flowInstanceId;
            req.Id = YitIdHelper.NextId();
            req.CreatedUserId = _userManager.UserId;
            req.CreatedUserName = _userManager.User.Name;
            req.CreatedTime = DateTime.Now;
            await _rep.InsertAsync(req);
        }
        public async Task Edit(long flowInstanceId, string frmData)
        {
            var req = frmData.ToObject<FlcCustomForm>();
            req.FlowInstanceId = flowInstanceId;
            await _rep.Context.Updateable<FlcCustomForm>(a => new FlcCustomForm
            {
                Remark = a.Remark,
                Sort = a.Sort,
                Name = a.Name

            }).Where(a => a.FlowInstanceId == req.FlowInstanceId).ExecuteCommandAsync();
        }
    }
}
